(function($){
	$.fn.hsupload = function(options) {
		// 创建一些默认值, 用提供的options来扩展
	    var settings = $.extend( {
	    	"defaultIcoClass" : "hs-ico-default",	//默认文件图标
	    	"fileTypeExts" : [						//限制上传类型
	    		"*.*"
	    	],					
	    	"height" : "130px",
	    	"icoClass" : {							//文件图标
	    		"gif" : "hs-ico-photo",
	    		"jpg" : "hs-ico-photo",
	    		"png" : "hs-ico-photo",
	    		"txt" : "hs-ico-txt" 				
    		},
	    	"name" : "mfile",						//上传名称
	    	//"inputName" : "",						//页面生成hidden的名称
	    	"limit" : 0,						//上传文件数量
	    	"url": "",
	    	"width" : "130px"
	    }, options);
	    
	    var filecount = 0;
	    
//	    /* 设置参数 属性配置复写js配置 */
//	    settings.fileTypeExts = this.attr("fileTypeExts") != undefined ? this.attr("fileTypeExts").split(",") : settings.fileTypeExts;
//	    //input name名
//	    settings.inputName = this.attr("inputName") != undefined ? this.attr("inputName") : settings.inputName;
//	    //上传数量限制
//	    settings.limit = this.attr("limit") != undefined ? parseInt(this.attr("limit")) : settings.limit;
//	   
//	    /* 验证 */
//	    if (settings.inputName === "") {
//	    	this.after($("<label>Must give a inputName to file taget or setting in plus!</label>"));
//	    	return;
//	    }
	    
	    //初始化检查
	    if (!initCheck(this)) {
	    	return this;
	    }
	    //创建上传控件
	    var $uploader = $("<div class='hsupload' />").replaceAll(this).append(this);
	    var $files = $("<ul/>").appendTo($uploader);
	    //添加已有文件
	    this.children().each(function(){
	    	addFileContainer($(this).text(), $(this).val());
	    });
    	
//	    if (window.FormData) {
//    		//增加一个上传按钮
//    	    var $inputFile = $("<input type='file' />").attr("name", settings.name);
//    	    $("<div />").append($inputFile).appendTo($uploader);
//    	    //inputFileOnChange($inputFile);
//    	} else {
    		//iframe 上传
    		var $form = $("<form enctype=\"multipart/form-data\" method=\"post\" target=\"hs_hidden_frame\" >" +
    						"<script type=\"text/javascript\">function a(){" +
    							"" +
    							"" +
    						"}</script>" +
    						"<input type=\"hidden\" name=\"html4\" value=\"true\" />" +
	    					"<iframe name=\"hs_hidden_frame\" id=\"hidden_frame\" style=\"display:none\" onload=\"a()\" >" +
	    					"<script type=\"text/javascript\">" +
	    						"function a() {" +
	    							"alert(123);" +
	    						"}" +
	    					"</script></iframe>" +
	    				"</form>");
    		$form.attr("action", settings.url);
    		var $inputFile = $("<input type=\"file\" />").attr("name", settings.name).appendTo($form);
    		$("<div />").append($form).appendTo($uploader);
    		inputFileOnChange($inputFile);
//	    }
	    
	    function inputFileOnChange($inputFile) {
	    	$inputFile.on("change", function() {
//		    	if (window.FormData) {
//		    	 
//		    	} else {
		    		var $this = $(this).parent().submit();
		    		addFileContainer($this.val(), $this.val());
//		    	}
	    	});
	    }
	    
	    /*
	    //设置控件外容器
	    var $mainContainer = $("<div />").replaceAll(this).addClass("hsupload");
	    //设置文件容器
	    var $filesContainer = $("<ul />").css("left", "0px").addClass("hs-files").appendTo($mainContainer);
	    
	    //将已有文件添加到文件容器中
	    this.children().each(function(){
	    	filecount++;
	    	var url = $(this).text();
	    	var filename = url.substring(url.lastIndexOf("/") + 1);
	    	addFileContainer(filename, url);
	    });
	    
	    //获得外容器宽度
	    var containerWidth = parseInt($mainContainer.css("width")) ;
	    //增加一个上传按钮
	    var $file = $("<input type='file' />").attr("name", settings.name).appendTo($mainContainer);
	    //this.appendTo($mainContainer);
	    
	    $("<div class='hs-leftarrow'>&lt;&lt;</div>").appendTo($mainContainer).css("top", "0px").css("left", "0px").css("opacity", 0)
	    	.hover(function(){	    		
	    				$(this).fadeTo("fast", 0.5);
	    			}, function() {
	    				$(this).fadeTo("fast", 0);
	    			}
	    	).on("click", function(){
	    		var left = parseInt($filesContainer.css("left"));
	    		console.log(left);
	    		//if (width + left)
	    		$filesContainer.animate({
	    			"left": left - containerWidth
	    		}, "slow");
	    	});
	    $("<div class='hs-rightarrow'>&gt;&gt;</div>").appendTo($mainContainer).css("top", "0px").css("right", "0px").fadeToggle("fast");
	    //var newFileTag = $("<input type='file' />").attr('id', id); 
	    
	    //if (typeof(FormData) !== "undefined") {
	    if (window.FormData) {
	    	//html5 ajax上传  
	    	$file.on("change", function() {
	    		if (settings.limit > 0 && settings.limit <= filecount) {
	    			alert("只能同时上传 " + settings.limit + " 个文件");
	    			return false;
	    		}
	    		
	    		for (var i = 0; i < this.files.length; i++) {	    			
	    			if (!checkFileType(this.files[i])) {
	    				alert("只能上传 " + settings.fileTypeExts + " 类型的文件");
	    				return false;
	    			}
	    		} 
	    		//设置上传文件名
	    		for(var i = 0; i < this.files.length; i++) {
	    			var file = this.files[i];
	    			//上传文件
			    	var formData = new FormData();
			    	formData.append(settings.name, file);
			    	var request = new XMLHttpRequest();
			    	request.open('POST', settings.url);
			    	//设置进度条
			    	//var $progress = $("<div class='hs-progress'>").appendTo($fileContainer);
			    	//request.upload.hs_progress = $progress;
			    	//进度条执行时事件
//			    	request.upload.onprogress = function(event) {
//			    		if (event.lengthComputable) {
//			    			var complete = (event.loaded / event.total * 100 | 0);
//			    			this.hs_progress.css("width", complete + "%");
//			    		}
//			    	};
			    	//上传成功后事件
			    	request.onload = function () {
			    		if (request.readyState == 4) {
				    		if (request.status === 200) {
				    			filecount++;
				    			var url = this.responseText;
				    			addFileContainer(file.name, url);
				    			console.log('上传成功');
				    		} else {
				    			console.log('出错了');
				    		}
				    	}
			    	};
			    	request.send(formData);
	    		}
	    	});
    	} else {
    		//iframe 上传  		
    		var $form = $("<form enctype=\"multipart/form-data\" method=\"post\" target=\"hs_hidden_frame\" >" +
	    					"<iframe name=\"hs_hidden_frame\" id=\"hidden_frame\" style=\"display:none\"></iframe>" +
	    				"</form>");
    		var $inputFile = $("<input type=\"file\" />").attr("name", settings.inputName);
			$form.append($inputFile);
	    } 	
	    
	    //settings.
    	
    	return $mainContainer;
    	*/

    	/**
	     * 初始化检查
	     * return true 通过; false 未通过
	     */
	    function initCheck($sel) {
	    	if (!$sel.is("select")) {
	    		$sel.after($("<label>The tag must be a &lt;select&gt;!</label>"));
		    	return false;
		    }
	    	return true;
	    }
    	
    	/**
	     * 创建文件容器
	     */
	    function addFileContainer(filename, url) {

	    	var extend = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
	    	
	    	var $fileContainer = $("<li class='hs-file' />");
	    	var input = $("<input type='hidden' />").attr("name", settings.inputName).val(url).appendTo($fileContainer);
	    	var $fileNameLabel = $("<div class='hs-filename' />").text(filename).appendTo($fileContainer);
	    	
	    	//设置工具栏
	    	var $toolBar = $("<div class='hs-toolbar' />").css('opacity', 0.45).appendTo($fileContainer);
	    	$fileContainer.hover(function(){ 
	    			$toolBar.toggle();
	    			$fileNameLabel.css("z-index", "6");
	    		}, function(){ 
	    			$toolBar.toggle();
	    			$fileNameLabel.css("z-index", "");
	    		});
	    	//删除按钮
	    	var $deleteTb = $("<div class='hs-toolbar-delete' />").appendTo($toolBar).on("click", function(){
	    		$fileContainer.fadeOut("fast", function() {
	    			$fileContainer.remove();
	    			filecount--;
	    		});
	    	});
	    	//设置文件类型图标
	    	var cssClass = settings.icoClass[extend] != null ? settings.icoClass[extend] : settings.defaultIcoClass;
	    	$fileContainer.addClass(cssClass);
	    	$fileContainer.css("width", settings.width);
	    	$fileContainer.css("height", settings.height);
	    	$fileContainer.attr("title", filename);
	    	$fileNameLabel.css("width", pxSub(settings.width, "30px"));
	    	$toolBar.css("width", settings.width);
	    	$files.append($fileContainer);
 	    }

	    /**
	     * 验证文件类型
	     */
	    function checkFileType(file) {
	    	for (var i = 0; i < settings.fileTypeExts.length; i++) {
				var pattern = new RegExp("." + settings.fileTypeExts[i] + "$", "gi");
				if (pattern.test(file.name)) {
					return true;
				}
			}
	    	return false;
	    }
	    
	    /**
	     * px相加
	     */
	    function pxAdd(t1, t2) {
	    	var n1 = parseInt(t1);
	    	var n2 = parseInt(t2);
	    	return n1 + n2 + "px";
	    }
	    
	    /**
	     * px相减
	     */
	    function pxSub(t1, t2) {
	    	var n1 = parseInt(t1);
	    	var n2 = parseInt(t2);
	    	return n1 - n2 + "px";
	    }
    };
}(jQuery));